/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef MYLOGGERWINDOW_H_
#define MYLOGGERWINDOW_H_

#include <Qt>
#include <QTextBrowser>
#include <QWidget>
#include <QScrollBar>
#include <QShortcut>

#include <string>
#include <iostream>

#include "ui_WinLogWidget.h"

using namespace std;

class WinLogWidget:public QWidget
{
	Q_OBJECT

private:
	Ui::WinLogWidget* mLogWindow;
	QWidget* mLoggerWidget;
	QTextBrowser* mLoggerBrowser;

	int mMaxLineCount;
	int mCurLineCount;
	float mAbridgeRate;

public:
	WinLogWidget(QWidget *parent = 0):
		QWidget(parent),
		mMaxLineCount(1000),
		mCurLineCount(0),
		mAbridgeRate(0.5)
	{
		mLoggerWidget = new QWidget();
		mLoggerWidget->setWindowFlags(Qt::WindowStaysOnTopHint);
		mLogWindow = new Ui::WinLogWidget;
		mLogWindow->setupUi(mLoggerWidget);
		initMyConnection();

		mLoggerBrowser = mLogWindow->loggerBrowser;
		mLoggerBrowser->setReadOnly(true);
		mLoggerBrowser->setUndoRedoEnabled(false);

		//cout<<"WinLogWidget::WinLogWidget-->mLoggerBrowser->verticalScrollBar()->maximumHeight() = "<<mLoggerBrowser->verticalScrollBar()->maximumHeight()<<endl;
		//mLoggerBrowser->verticalScrollBar()->setValue(mLoggerBrowser->verticalScrollBar()->maximumHeight());
	}

	void initMyConnection()
	{
		QShortcut* closeShortcut = new QShortcut(QKeySequence(Qt::Key_Escape),  this);
		QObject::connect(closeShortcut,SIGNAL(activated()),this,SLOT(hide()));

		 QObject::connect(mLogWindow->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
		 QObject::connect(mLogWindow->closeButton, SIGNAL(clicked()), this, SLOT(hide()));
		 //QObject::connect(mLogWindow->closeButton, SIGNAL(clicked()), this, SLOT(hide()));
	}

	void insertText(string text)
	{
		mCurLineCount++;
		//cout<<"WinLogWidget::WinLogWidget-->mCurLineCount = "<<mCurLineCount<<endl;
		if (mCurLineCount >= mMaxLineCount)
		{
			QString context = mLogWindow->loggerBrowser->toPlainText();
			mLogWindow->loggerBrowser->clear();
			context.remove(0, int(context.size()*mAbridgeRate));
			mCurLineCount = int(mCurLineCount*mAbridgeRate);
			mLogWindow->loggerBrowser->insertPlainText(context);
			//cout<<"WinLogWidget::insertText-->text browser buffer abridged."<<endl;
		}
		mLogWindow->loggerBrowser->insertPlainText(text.c_str());
		int buttonLine = mLogWindow->loggerBrowser->verticalScrollBar()->maximumHeight();
		//cout<<"WinLogWidget::WinLogWidget-->buttonLine = "<<buttonLine<<endl;
		mLogWindow->loggerBrowser->verticalScrollBar()->setValue(buttonLine);
	}

public slots:

	void show()
	{
		mLoggerWidget->show();
	}

	void hide()
	{
		mLoggerWidget->hide();
	}

	void clear()
	{
		mLoggerBrowser->clear();
		mCurLineCount = 0;
	}
};


#endif /* MYLOGGERWINDOW_H_ */
